<!-- 发生短信 -->
<template>
    <el-row class="user-info-form" type="flex" justify="center">
        <el-col>
            <el-form label-position="top" :model="editForm" :rules="editFormRules" ref="editForm" @submit.native.prevent>
                <el-form-item label="邮件标题：" prop="title">
                    <el-input v-model="editForm.title" auto-complete="off" placeholder="请输入邮件标题" type="textarea" clearable></el-input>
                </el-form-item>
                <el-form-item label="收件人：" prop="sendMailArr">
                  <el-select filterable allow-create v-model="sendMailArr" multiple>
                    <el-option
                      v-for="(item,index) in emailData"
                      :key="index"
                      :label="item.name"
                      :value="item.email.toString()">
                    </el-option>
                  </el-select>
                </el-form-item>
                <el-form-item label="邮件内容：" prop="content">
                    <el-input v-model="editForm.content" auto-complete="off" placeholder="请输入邮件内容" type="textarea" clearable></el-input>
                </el-form-item>
                <el-form-item label-width="0" class="verifyidentity-button">
                    <el-button @click="closeDialog()">取消</el-button>
                    <el-button type="primary" @click="savePointType('editForm')">确定</el-button>
                </el-form-item>
            </el-form>
        </el-col>
    </el-row>
</template>

<script>
import util from '@/utils/util'
import { getPInfo} from '@/api/org'
import { getOPC,sendEmail} from '@/api/communicat'
import { validEmail } from '@/utils/validate'

export default {
  name:'SendMsg',
  props: {
    editInfo: {
        type: Object,
        required: false
    },
    closeDialog: {
        type: Function,
        required: true
    },
  },
  data() {
    const validaterecipients = (rule, value, callback) => {
      let _arr = this.sendMailArr
      if(_arr.length<1){
        callback(new Error('收件人不能为空'))
      }else {
        let flag = true
        $.each(_arr,(index,num)=>{
          if(!validEmail(num)){
            flag = false
            return false
          }
        })
        if(!flag)
          callback(new Error('请输入正确的邮箱地址'))
        callback()
      }
    }
    return {
      sendMailArr:[],
      emailData:[],//电话号码集合
      util:util,
      editForm: {
          organizationId:this.$store.state.userinfo.org.id,
          title:'操作平台通讯录',
          recipients:'',
          content:'',
          type: 3
      },
      editFormRules: {
        sendMailArr: [
            {  required: true,validator: validaterecipients  ,trigger: ['blur', 'change']}
        ],
        title: [
            { required: true, message: '请输入邮件标题', trigger: 'blur' },
        ],
        content: [
            { required: true, message: '请输入邮件内容', trigger: 'blur' },
        ],
      },
    }
  },
  created() {
      let infoObj = this.editInfo
      let that = this
      if(infoObj.id) {//编辑
         that.editForm.organizationId = infoObj.organizationId || infoObj.orgId
         that.editForm.recipients = validEmail(infoObj.email)?infoObj.email:''
         that.sendMailArr = infoObj.email?infoObj.email.toString().split(','):[]
         that.editForm.content = infoObj.content || ''
      }
      this.getData()
  },
  methods: {
    getData() {
      let that = this
      getPInfo({
        organizationId : that.editForm.organizationId,
        roleIds :""
      }).then(res => {
        $.each(res.data,(index,obj)=>{
          if(validEmail(obj.email)){
            that.emailData.push({
              email:obj.email,
              name:obj.roleName + obj.name
            })
          }
        })
      })

    },
    savePointType(formName) {
        // 阻止事件冒泡
        event.stopPropagation()
        let that = this
        that.$refs[formName].validate((valid) => {
            if (valid) {
                let parames = that.editForm
                parames.recipients = that.sendMailArr.toString()
                sendEmail(parames).then(res => {
                  if( res.status === 200) {
                    that.$message({
                        message: '发送成功！',
                        type: 'success'
                    })
                  }else{
                    this.$message.error(res.info);
                  }
                  that.closeDialog()
                });
            } else {
                return false
            }
        });
    },
  }
}
</script>